/**
 * Created by Administrator on 2017/12/19.
 */
import cookie from "js-cookie";
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';
import { orgchildren,organization } from '../../services/organization';
import { particulars } from '../../services/Personalinformation'

export default {
  namespace: 'management',
  state: {
    orgLists:[],
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({pathname, query}) => {
        console.log(pathname);
        if(pathname === '/work/dyadminister') {
          dispatch({type:'fetch',})
          dispatch({type:'audit',})
        }
      })
    }
  },

  effects: {
    *fetch({payload}, { call, put, select }) {
      let newsObject = sessionStorage.getItem('userdata');
      newsObject = JSON.parse(newsObject);
      const data =yield call(orgchildren,{code:newsObject[1].orgs[0][0].org_code});
      let data0=[];
      if (data.data.code == 200) {
          for (let i in data.data.ret) {
            data0.push(data.data.ret[i].name)
        }
        yield put({
          type: 'success',
          payload: {
            orgLists: data0
          }
        });
        if (data.ret.data.length < 1) {
          Toast.fail("没有数据", 1);
        }
      }
      else {
        Toast.success('查询失败！', 1);
      }

    },
    //获取党支部
    *orgBottom({payload}, { call, put, select }) {
      console.log("payload",payload);
      const data = yield call(organization, {code:payload.code?payload.code:"",type_code: '631'});
      if (data.data.code == 200) {
        yield put({
          type: 'success',
          payload: {
            orgBottom: data.data.ret
          }
        });
      }
      else {
        Toast.success('查询失败！', 1);
      }
    },
    *audit({payload}, { call, put, select }) {
      let userdata = sessionStorage.getItem("userdata");
      let c = JSON.parse(userdata);
      let data = yield call(particulars, {org_code:c[1].orgs[0][0].org_code,relation:["org","extend ","infos","memberPartyPrice "]});
      let num=data.data.ret.length;//党员总数
      let a=data.data.ret;
      let b=[];
      let e=[];
      let d=[];
      let f=[];
      let EthnicMinorities,men,women,Aformal;
      for (let i=0;i<a.length;i++){
        if (a[i].nation!=="汉族"){
          b.push(a[i].nation);
          EthnicMinorities=b.length;//少数民族人数
        }
        if (a[i].sex_code==1){
          e.push(a[i].sex_code);
          men=e.length;
        }else {
          d.push(a[i].sex_code);
          women=d.length;
        }
        if (a[i].type=="正式党员"){
          f.push(a[i].type);
          Aformal=f.length;
        }
      }
      let percentage = parseInt(Aformal/num*100)+"%";//百分比
      yield put({
        type: 'success',
        payload: {
          num:num,
          EthnicMinorities:EthnicMinorities,
          men:men,
          women:women,
          percentage:percentage
        }
      })
    }
  },
  reducers: {
    success(state, action){
      return {...state, ...action.payload};
    }
  }
};
